package com.cheyun.netsalev3.tencenttim.helper;

import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.cheyun.netsalev3.MyApplication;
import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.tencenttim.chat.ChatActivity;
import com.cheyun.netsalev3.tencenttim.signature.GenerateTestUserSig;
import com.cheyun.netsalev3.tencenttim.utils.Constants;
import com.cheyun.netsalev3.tencenttim.utils.DemoLog;
import com.cheyun.netsalev3.utils.MyAudioPlayer;
import com.cheyun.netsalev3.utils.api.DmApiClient;
import com.cheyun.netsalev3.utils.api.DmBaseResp;
import com.cheyun.netsalev3.utils.api.dm.DmApiCallback;
import com.cheyun.netsalev3.utils.api.dm.DmApiException;
import com.google.gson.Gson;
import com.tencent.imsdk.TIMConversation;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMManager;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.qcloud.tim.uikit.modules.chat.C2CChatManagerKit;
import com.tencent.qcloud.tim.uikit.modules.chat.ChatLayout;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.chat.layout.message.holder.ICustomMessageViewGroup;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfoUtil;
import com.tencent.qcloud.tim.uikit.utils.DateTimeUtil;
import com.tencent.qcloud.tim.uikit.utils.TUIKitLog;
import com.tencent.trtc.TRTCCloud;
import com.tencent.trtc.TRTCCloudListener;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;
import java.util.Random;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.cheyun.netsalev3.tencenttim.helper.CustomMessage.JSON_VERSION_3_ANDROID_IOS_TRTC;
import static com.cheyun.netsalev3.tencenttim.helper.CustomMessage.VIDEO_CALL_ACTION_ACCEPTED;
import static com.cheyun.netsalev3.tencenttim.helper.CustomMessage.VIDEO_CALL_ACTION_DIALING;
import static com.cheyun.netsalev3.tencenttim.helper.CustomMessage.VIDEO_CALL_ACTION_HANGUP;
import static com.cheyun.netsalev3.tencenttim.helper.CustomMessage.VIDEO_CALL_ACTION_LINE_BUSY;
import static com.cheyun.netsalev3.tencenttim.helper.CustomMessage.VIDEO_CALL_ACTION_REJECT;
import static com.cheyun.netsalev3.tencenttim.helper.CustomMessage.VIDEO_CALL_ACTION_SPONSOR_CANCEL;
import static com.cheyun.netsalev3.tencenttim.helper.CustomMessage.VIDEO_CALL_ACTION_SPONSOR_TIMEOUT;

public class CustomAVCallUIController2 extends TRTCCloudListener {

    private static final String TAG = CustomAVCallUIController2.class.getSimpleName();

    private static final int VIDEO_CALL_STATUS_FREE = 1;
    private static final int VIDEO_CALL_STATUS_BUSY = 2;
    private static final int VIDEO_CALL_STATUS_WAITING = 3;
    private int mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;

    private static CustomAVCallUIController2 mController;

    private long mEnterRoomTime;
    private CustomMessage mOnlineCall;
    private CustomMessage waitMessage;
    private ChatLayout mUISender;
    private TRTCDialog mDialog;
    private TRTCCloud mTRTCCloud;

    private static final int VIDEO_CALL_OUT_GOING_TIME_OUT = 60 * 1000;
    private static final int VIDEO_CALL_OUT_INCOMING_TIME_OUT = 60 * 1000;
    private Handler mHandler = new Handler();
    private Runnable mVideoCallOutgoingTimeOut = new Runnable() {
        @Override
        public void run() {
            DemoLog.i(TAG, "time out, dismiss outgoing dialog");
            mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
            sendVideoCallAction(VIDEO_CALL_ACTION_SPONSOR_CANCEL, mOnlineCall);
            dismissDialog();
        }
    };

    private Runnable mVideoCallIncomingTimeOut = new Runnable() {
        @Override
        public void run() {
            DemoLog.i(TAG, "time out, dismiss incoming dialog");
            mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
            sendVideoCallAction(VIDEO_CALL_ACTION_SPONSOR_TIMEOUT, mOnlineCall == null? waitMessage:mOnlineCall);
            dismissDialog();
        }
    };

    private CustomAVCallUIController2() {
        mTRTCCloud = TRTCCloud.sharedInstance(MyApplication.getInstance());
        TRTCListener.getInstance().addTRTCCloudListener(this);
        mTRTCCloud.setListener(TRTCListener.getInstance());
    }

    public static CustomAVCallUIController2 getInstance() {
        if (mController == null) {
            mController = new CustomAVCallUIController2();
        }
        return mController;
    }

    public void onCreate() {
        mTRTCCloud = TRTCCloud.sharedInstance(MyApplication.getInstance());
        mTRTCCloud.setListener(this);
    }

    @Override
    public void onError(int errCode, String errMsg, Bundle extraInfo) {
        DemoLog.i(TAG, "trtc onError");
        mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
        sendVideoCallAction(VIDEO_CALL_ACTION_HANGUP, mOnlineCall);
        Toast.makeText(MyApplication.getInstance().getBaseContext(), "通话异常: " + errMsg + "[" + errCode + "]", Toast.LENGTH_LONG).show();
        CustomAVCallUIController2.getInstance().exitRoom();
        if (mTRTCCloud != null) {
            mTRTCCloud.exitRoom();
        }
    }

    @Override
    public void onEnterRoom(long elapsed) {
        DemoLog.i(TAG, "onEnterRoom " + elapsed);
        Toast.makeText(MyApplication.getInstance().getBaseContext(), "开始通话", Toast.LENGTH_SHORT).show();
        mEnterRoomTime = System.currentTimeMillis();
    }

    @Override
    public void onExitRoom(int reason) {
        DemoLog.i(TAG, "onExitRoom " + reason);
        Toast.makeText(MyApplication.getInstance().getBaseContext(), "结束通话", Toast.LENGTH_SHORT).show();
        mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
    }

//    public void setUISender(ChatLayout layout) {
//        DemoLog.i(TAG, "setUISender: " + layout);
//        mUISender = layout;
//        if (mCurrentVideoCallStatus == VIDEO_CALL_STATUS_WAITING) {
//            boolean success = showIncomingDialingDialog();
//            if (success) {
//                mCurrentVideoCallStatus = VIDEO_CALL_STATUS_BUSY;
//                AssetManager assetManager = mUISender.getContext().getResources().getAssets();
//                try {
//                    final AssetFileDescriptor fileDescriptor = assetManager.openFd("audio.wav");
//                    MyAudioPlayer.getInstance().startPlay(fileDescriptor,null);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            } else {
//                mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
//                sendVideoCallAction(VIDEO_CALL_ACTION_REJECT, mOnlineCall);
//                Toast.makeText(mUISender.getContext(), "发起通话失败，没有弹出对话框权限", Toast.LENGTH_SHORT).show();
//            }
//        }
//    }
    public void setUISender(ChatLayout layout) {
        DemoLog.i(TAG, "setUISender: " + layout);
        mUISender = layout;
//        if (mCurrentVideoCallStatus == VIDEO_CALL_STATUS_WAITING) {
//            boolean success = showIncomingDialingDialog();
//            if (success) {
//                mCurrentVideoCallStatus = VIDEO_CALL_STATUS_BUSY;
//                AssetManager assetManager = mUISender.getContext().getResources().getAssets();
//                try {
//                    final AssetFileDescriptor fileDescriptor = assetManager.openFd("audio.wav");
//                    MyAudioPlayer.getInstance().startPlay(fileDescriptor,null);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            } else {
//                mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
//                sendVideoCallAction(VIDEO_CALL_ACTION_REJECT, mOnlineCall);
//                Toast.makeText(mUISender.getContext(), "发起通话失败，没有弹出对话框权限", Toast.LENGTH_SHORT).show();
//            }
//        }
    }

    public void onDraw(ICustomMessageViewGroup parent, CustomMessage data) {
        // 把自定义消息view添加到TUIKit内部的父容器里
        View view = LayoutInflater.from(MyApplication.getInstance()).inflate(R.layout.test_custom_message_av_layout1, null, false);
        parent.addMessageContentView(view);

        if (data == null) {
            DemoLog.i(TAG, "onCalling null data");
            return;
        }
        TextView textView = view.findViewById(R.id.test_custom_message_tv);

        String callingAction = "";

//        switch (data.action) {
        switch (data.videoState) {
            // 新接一个电话
            case VIDEO_CALL_ACTION_DIALING:
                callingAction = "[请求通话]";
                break;
            case VIDEO_CALL_ACTION_SPONSOR_CANCEL:
                callingAction = "[取消通话]";
                break;
            case VIDEO_CALL_ACTION_REJECT:
                callingAction = "[拒绝通话]";
                break;
            case VIDEO_CALL_ACTION_SPONSOR_TIMEOUT:
                callingAction = "[无应答]";
                break;
            case VIDEO_CALL_ACTION_ACCEPTED:
                callingAction = "[开始通话]";
                break;
            case VIDEO_CALL_ACTION_HANGUP:
                callingAction = "[结束通话，通话时长：" + DateTimeUtil.formatSeconds(data.duration) + "]";
                break;
            case VIDEO_CALL_ACTION_LINE_BUSY:
                callingAction = "[正在通话中]";
                break;
            default:
                DemoLog.e(TAG, "unknown data.action: " + data.action);
                callingAction = "[不能识别的通话指令]";
                break;
        }
        textView.setText(callingAction);
    }

    public void createVideoCallRequest() {
        // 显示通话UI
        boolean success = showOutgoingDialingDialog();
        if (success) {
            mCurrentVideoCallStatus = VIDEO_CALL_STATUS_BUSY;
            CustomMessage roomInfo = new CustomMessage();
            assembleOnlineCall(null);
            sendVideoCallAction(VIDEO_CALL_ACTION_DIALING, mOnlineCall);
            mHandler.removeCallbacksAndMessages(null);
            mHandler.postDelayed(mVideoCallOutgoingTimeOut, VIDEO_CALL_OUT_GOING_TIME_OUT);
        } else {
            Toast.makeText(mUISender.getContext(), "发起通话失败，没有弹出对话框权限", Toast.LENGTH_SHORT).show();
        }
    }

    public void hangup() {
        DemoLog.i(TAG, "hangup");
        mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
        sendVideoCallAction(VIDEO_CALL_ACTION_HANGUP, mOnlineCall);
    }

    public void close(){
        mTRTCCloud.exitRoom();
        CustomAVCallUIController2.getInstance().exitRoom();
        mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
    }
    public void exitRoom(){
        String phone = TIMManager.getInstance().getLoginUser();
        DmApiClient.INSTANCE.getInstance().kefuRunningDel(phone)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new DmApiCallback<DmBaseResp<Object, Object>>() {
                    @Override
                    protected void onSuccess(DmBaseResp<Object, Object> t) {
                        DemoLog.i(TAG, "del kefu success" );
                    }

                    @Override
                    protected void onError(@NotNull DmApiException ex) {
                        DemoLog.i(TAG, "del kefu fail" );
                    }
                });
    }

    private void enterRoom() {
//        final Intent intent = new Intent(mUISender.getContext(), TRTCActivity.class);
//        intent.putExtra(TRTCActivity.KEY_ROOM_ID, mOnlineCall.room_id);
//        mUISender.getContext().startActivity(intent);

//        MyApplication.getInstance().getTimAcitvitys().startCall(mOnlineCall.roomID,mOnlineCall.onlyAudio==0?1:2);

//        ChatInfo chatInfo = new ChatInfo();
//        chatInfo.setType(TIMConversationType.C2C);
//        chatInfo.setId(mOnlineCall.getPartner());
//        chatInfo.setChatName(mOnlineCall.getPartner());
//
//        DemoLog.i(TAG, "销毁已存在聊天界面");
//        MyApplication.getInstance().removeChatActivity();
//
//        Intent intent = new Intent(MyApplication.getInstance(), ChatActivity.class);
//        intent.putExtra(Constants.CHAT_INFO, chatInfo);
//        intent.putExtra(Constants.CHAT_TYPE,mOnlineCall.onlyAudio==0?1:2);
//        intent.putExtra(Constants.ROOM_ID,mOnlineCall.roomID);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        MyApplication.getInstance().startActivity(intent);
    }

    public void sendVideoCallAction(int action, CustomMessage roomInfo) {
        DemoLog.i(TAG, "sendVideoCallAction action: " + action
                + " call_id: " + roomInfo.call_id
                + " room_id: " + roomInfo.room_id
                + " partner: " + roomInfo.getPartner());
        Gson gson = new Gson();
        CustomMessage message = new CustomMessage();
        message.version = JSON_VERSION_3_ANDROID_IOS_TRTC;
        message.call_id = roomInfo.call_id;
        message.room_id = roomInfo.roomID;
        message.roomID = roomInfo.roomID;
        message.videoState = action;
        message.action = action;
        message.invited_list = roomInfo.invited_list;
        if (action == VIDEO_CALL_ACTION_HANGUP) {
            message.duration = (int) (System.currentTimeMillis() - mEnterRoomTime + 500) / 1000;
        }
        String data = gson.toJson(message);
        MessageInfo info = MessageInfoUtil.buildCustomMessage(data);
        if (TextUtils.equals(mOnlineCall == null?waitMessage.getPartner(): mOnlineCall.getPartner(), roomInfo.getPartner())) {
            if(mUISender!=null){
                mUISender.sendMessage(info, false);
            }else{
                ChatInfo chatInfo = new ChatInfo();
                chatInfo.setType(TIMConversationType.C2C);
                chatInfo.setId(message.getPartner());
                chatInfo.setChatName(message.getPartner());
                C2CChatManagerKit.getInstance().setCurrentChatInfo(chatInfo);
                C2CChatManagerKit.getInstance().sendMessage(info,false,null);
            }
        } else {
            TIMConversation con = TIMManager.getInstance().getConversation(TIMConversationType.C2C, roomInfo.getPartner());
            con.sendMessage(info.getTIMMessage(), new TIMValueCallBack<TIMMessage>() {

                @Override
                public void onError(int code, String desc) {
                    DemoLog.i(TAG, "sendMessage fail:" + code + "=" + desc);
                }

                @Override
                public void onSuccess(TIMMessage timMessage) {
                    TUIKitLog.i(TAG, "sendMessage onSuccess");
                }
            });
        }
    }

    private String createCallID() {
        final String CHARS = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        sb.append(GenerateTestUserSig.SDKAPPID).append("-").append(TIMManager.getInstance().getLoginUser()).append("-");
        for (int i = 0; i < 32; i++) {
            int index = random.nextInt(CHARS.length());
            sb.append(CHARS.charAt(index));
        }
        return sb.toString();
    }
    private int getRandomInt(int min ,int max){
        Random random = new Random();
        int i = random.nextInt(max - min) + min;
        return i;
    }
    private void assembleOnlineCall(CustomMessage roomInfo) {
        mOnlineCall = new CustomMessage();
        if (roomInfo == null || roomInfo.call_id.isEmpty()) {
            mOnlineCall.call_id = createCallID();
            mOnlineCall.room_id = getRandomInt(0,42949);//  new Random().nextInt();
            mOnlineCall.roomID = mOnlineCall.room_id ;
            mOnlineCall.invited_list = new String[] {mUISender.getChatInfo().getId()};
            mOnlineCall.setPartner(mUISender.getChatInfo().getId());
        } else {
            mOnlineCall.call_id = roomInfo.call_id;
            mOnlineCall.room_id = roomInfo.roomID>0? roomInfo.roomID:roomInfo.room_id;
            mOnlineCall.roomID =roomInfo.roomID>0? roomInfo.roomID:roomInfo.room_id;
            mOnlineCall.invited_list = roomInfo.invited_list;
            mOnlineCall.setPartner(roomInfo.getPartner());
            mOnlineCall.onlyAudio = roomInfo.onlyAudio;
            mOnlineCall.chatType = roomInfo.onlyAudio==1?2:1;
        }
    }

    public void onNewMessage(List<TIMMessage> msgs) {
        CustomMessage data = CustomMessage.convert2VideoCallData(msgs);
        if (data != null) {
            onNewComingCall(data);
        }
    }

    private void onNewComingCall(CustomMessage message) {
        DemoLog.i(TAG, "onNewComingCall current state: " + mCurrentVideoCallStatus
                + " call_id action: " + message.action
                + " call_id videostate: " + message.videoState
                + " coming call_id: " + message.call_id
                + " coming room_id: " + message.room_id
                + " current room_id: " + (mOnlineCall == null ? null : mOnlineCall.room_id));
        waitMessage = message;
//        switch (message.action) {
        switch (message.videoState) {
            case VIDEO_CALL_ACTION_DIALING:
                if (mCurrentVideoCallStatus == VIDEO_CALL_STATUS_FREE) {
                    mCurrentVideoCallStatus = VIDEO_CALL_STATUS_WAITING;
                    boolean success = showIncomingDialingDialog();
                    if (success) {
//                        mCurrentVideoCallStatus = VIDEO_CALL_STATUS_BUSY;
                        AssetManager assetManager = MyApplication.getInstance().getBaseContext().getResources().getAssets();
                        try {
                            final AssetFileDescriptor fileDescriptor = assetManager.openFd("audio.wav");
                            MyAudioPlayer.getInstance().startPlay(fileDescriptor,null);
                        } catch (IOException e) {
                            Log.e(TAG,e.getMessage());
                        }
                    } else {
                        mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
                        sendVideoCallAction(VIDEO_CALL_ACTION_REJECT, message);
                        Toast.makeText(MyApplication.getInstance().getBaseContext(), "发起通话失败，没有弹出对话框权限", Toast.LENGTH_SHORT).show();
                    }

//                    startC2CConversation(message);
//                    assembleOnlineCall(message);
                } else {
                    sendVideoCallAction(VIDEO_CALL_ACTION_LINE_BUSY, message);
                }
                break;
            case VIDEO_CALL_ACTION_SPONSOR_CANCEL:
                if (mCurrentVideoCallStatus != VIDEO_CALL_STATUS_FREE && TextUtils.equals(message.call_id, mOnlineCall.call_id)) {
                    dismissDialog();
                    mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
                }
                break;
            case VIDEO_CALL_ACTION_REJECT:
                if (mCurrentVideoCallStatus != VIDEO_CALL_STATUS_FREE && TextUtils.equals(message.call_id, mOnlineCall.call_id)) {
                    dismissDialog();
                    mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
                }
                break;
            case VIDEO_CALL_ACTION_SPONSOR_TIMEOUT:
                if (mCurrentVideoCallStatus != VIDEO_CALL_STATUS_FREE && TextUtils.equals(message.call_id, mOnlineCall.call_id)) {
                    dismissDialog();
                    mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
//                    ChatActivity chatActivity = MyApplication.getInstance().getTimAcitvitys();
//                    if(chatActivity!=null){
//                        chatActivity.closeCall();
//                    }
                }
                break;
            case VIDEO_CALL_ACTION_ACCEPTED:
                if (mCurrentVideoCallStatus != VIDEO_CALL_STATUS_FREE && TextUtils.equals(message.call_id, mOnlineCall.call_id)) {
                    dismissDialog();
                }
                assembleOnlineCall(message);
                enterRoom();
                break;
            case VIDEO_CALL_ACTION_HANGUP:
                dismissDialog();
                mTRTCCloud.exitRoom();
                CustomAVCallUIController2.getInstance().exitRoom();
                mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
//                ChatActivity chatActivity = MyApplication.getInstance().getTimAcitvitys();
//                if(chatActivity!=null){
//                    chatActivity.closeCall();
//                }
                break;
            case VIDEO_CALL_ACTION_LINE_BUSY:
                if (mCurrentVideoCallStatus == VIDEO_CALL_STATUS_BUSY && TextUtils.equals(message.call_id, mOnlineCall.call_id)) {
                    dismissDialog();
                }
                break;
            default:
                DemoLog.e(TAG, "unknown data.action: " + message.action);
                break;
        }
    }

    private void startC2CConversation(CustomMessage message) {
        // 小米手机需要在安全中心里面把demo的"后台弹出权限"打开，才能当应用退到后台时弹出通话请求对话框。
//        if(MyApplication.getInstance().getTimAcitvitys()!=null){
////            if(MyApplication.getInstance().getTimAcitvitys().mChatInfo.getId() != message.getPartner()) {
////                MyApplication.getInstance().getTimAcitvitys().closeCall();
////                startNewCaht(message);
////            }else{
////
////            }
//            MyApplication.getInstance().getTimAcitvitys().closeCall();
//            startNewCaht(message);
//        }else{
//            startNewCaht(message);
//        }
        startNewCaht(message);
    }
    private void startNewCaht(CustomMessage message){
        DemoLog.i(TAG, "startC2CConversation " + message.getPartner());
        ChatInfo chatInfo = new ChatInfo();
        chatInfo.setType(TIMConversationType.C2C);
        chatInfo.setId(message.getPartner());
        chatInfo.setChatName(message.getPartner());
        Intent intent = new Intent(MyApplication.getInstance(), ChatActivity.class);
        intent.putExtra(Constants.CHAT_INFO, chatInfo);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Constants.CHAT_TYPE,message.chatType);
        intent.putExtra(Constants.ROOM_ID,message.roomID);
        MyApplication.getInstance().startActivity(intent);
    }

//    private boolean showIncomingDialingDialog() {
//        dismissDialog();
//        mHandler.removeCallbacksAndMessages(null);
//        mHandler.postDelayed(mVideoCallIncomingTimeOut, VIDEO_CALL_OUT_INCOMING_TIME_OUT);
//        mDialog = new TRTCDialog(mUISender.getContext());
//        mDialog.setTitle(mOnlineCall.onlyAudio==1?"请求语音通话":"请求视频通话");
//        mDialog.setPositiveButton("接听", new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                DemoLog.i(TAG, "VIDEO_CALL_ACTION_ACCEPTED");
//                mHandler.removeCallbacksAndMessages(null);
//                MyAudioPlayer.getInstance().stopPlay();
//                sendVideoCallAction(VIDEO_CALL_ACTION_ACCEPTED, mOnlineCall);
//                mCurrentVideoCallStatus = VIDEO_CALL_STATUS_BUSY;
//                enterRoom();
//            }
//        });
//        mDialog.setNegativeButton("拒绝", new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                DemoLog.i(TAG, "VIDEO_CALL_ACTION_REJECT");
//                mHandler.removeCallbacksAndMessages(null);
//                MyAudioPlayer.getInstance().stopPlay();
//                mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
//                sendVideoCallAction(VIDEO_CALL_ACTION_REJECT, mOnlineCall);
//            }
//        });
//        return mDialog.showSystemDialog();
//    }

    private boolean showIncomingDialingDialog() {
        dismissDialog();
        mHandler.removeCallbacksAndMessages(null);
        mHandler.postDelayed(mVideoCallIncomingTimeOut, VIDEO_CALL_OUT_INCOMING_TIME_OUT);
        mDialog = new TRTCDialog(MyApplication.getInstance().getBaseContext());
        mDialog.setTitle("["+waitMessage.getPartner()+"]"+(waitMessage.onlyAudio==1?"请求与您语音通话":"请求与您视频通话"));
        mDialog.setPositiveButton("接听", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DemoLog.i(TAG, "VIDEO_CALL_ACTION_ACCEPTED");
                mHandler.removeCallbacksAndMessages(null);
                MyAudioPlayer.getInstance().stopPlay();
                startC2CConversation(waitMessage);
                assembleOnlineCall(waitMessage);
                sendVideoCallAction(VIDEO_CALL_ACTION_ACCEPTED, mOnlineCall);
                mCurrentVideoCallStatus = VIDEO_CALL_STATUS_BUSY;
//                enterRoom();
            }
        });
        mDialog.setNegativeButton("拒绝", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DemoLog.i(TAG, "VIDEO_CALL_ACTION_REJECT");
                mHandler.removeCallbacksAndMessages(null);
                MyAudioPlayer.getInstance().stopPlay();
                mCurrentVideoCallStatus = VIDEO_CALL_STATUS_FREE;
                sendVideoCallAction(VIDEO_CALL_ACTION_REJECT, waitMessage);
            }
        });
        return mDialog.showSystemDialog();
    }


    private boolean showOutgoingDialingDialog() {
        dismissDialog();
        mDialog = new TRTCDialog(mUISender.getContext());
        mDialog.setTitle("等待对方接听");
        mDialog.setPositiveButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DemoLog.i(TAG, "VIDEO_CALL_ACTION_SPONSOR_CANCEL");
                mHandler.removeCallbacksAndMessages(null);
                sendVideoCallAction(VIDEO_CALL_ACTION_SPONSOR_CANCEL, mOnlineCall);
            }
        });
        return mDialog.showSystemDialog();
    }

    private void dismissDialog() {
        mHandler.removeCallbacksAndMessages(null);
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

}
